package createJfinalFileV1;

import createJfinalFileV1.write.FileWrite;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 马洁
 * @date 2019/11/15 - 20:02
 */
public class CreateFileMain {
    public static boolean writeFile(String packageName, String prefix, PrintWriter[] writers) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        new FileWrite(packageName, prefix, writers).write();
        return true;
    }

    public static boolean create(String path, String defaultPackage, String[] list) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        File dir = new File(path);

        for (String item:
                list) {

            File packageFile = new File(dir.getAbsolutePath() + "\\" + defaultPackage + "\\" + item);

            if (!packageFile.mkdir()) {
                return false;
            } else {
                File[] finalFiles = new File[Config.fileSuffix.length];

                for (int i = 0; i < Config.fileSuffix.length; i++) {
                    finalFiles[i] = new File(
                            packageFile.getPath() + "\\" + item + Config.fileSuffix[i] + Config.FILE_TYPE);
                    if (!finalFiles[i].createNewFile()) {
                        return false;
                    }
                }

                PrintWriter[] printWriters = new PrintWriter[Config.fileSuffix.length];
                int i = 0;
                for (File file:
                        finalFiles) {
                    printWriters[i++] = new PrintWriter(file, "utf-8");
                }

                writeFile(defaultPackage + "." + item, item, printWriters);
            }
        }

        return true;
    }
}
